<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>头痛小练习</title>
</head>
<body>
  <div id="test"></div>

  <!-- 引入库：先引入核心库，再引入操作DOM的扩展库 -->
  <script type="text/javascript" src="js/react.development.js"></script>
  <script type="text/javascript" src="js/react-dom.development.js"></script>
  <!-- 将jsx转为js -->
  <script type="text/javascript" src="js/babel.min.js"></script>
  <script type="text/javascript" src="js/prop-types.js"></script>
  <script type="text/babel">    //必须写babel，因为默认js
    class Person extends React.Component{
      state = {opacity:1}

      death = ()=>{
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      componentDidMount(){
        this.timer = setInterval(()=>{
          let {opacity} = this.state
          opacity -= 0.1
          if(opacity <= 0) opacity = 1
          this.setState({opacity})
        }, 200)
      }

      componentWillUnmount(){
        clearInterval(this.timer)
      }
      render(){
        return(
          <div>
            <h2 style={{opacity:this.state.opacity}}>欢迎欢迎</h2>
            <button onClick={this.death}>欢迎结束</button>
            <Account/>
          </div>
        )
      }
    }

    class Account extends React.Component{

      state = {
        username:'',
        password:''
      }

      saveFormData = (dataType)=>{
        return event => this.setState({[dataType]:event.target.value})
      }

      handleSubmint = (event)=>{
        event.preventDefault()
      }

      render(){
        return(
          <div>
            <form onSubmit={this.handleSubmint}>
              <input onChange={this.saveFormData('username')} type="text" placeholder="请输入账号" name="username"/>
              <input onChange={this.saveFormData('password')} type="password" placeholder="请输入密码" name="password"/><br/>
              <button>登录</button>
            </form>
            <Student username={this.state.username} password={this.state.password}/>
          </div>
        )
      }
    }

    class Student extends React.Component{
      render(){
        return(
          <h2>您输入的账号名为：{this.props.username}，密码为：{this.props.password}</h2>
        )
      }
    }

    ReactDOM.render(<Person/>, document.getElementById('test'))
  </script>
</body>
</html>